/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jaiz.twitter.controller;

import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import org.jaiz.twitter.model.Usuario;
import org.jaiz.twitter.service.LoginService;

/**
 *
 * @author JesusMata
 */
@WebServlet(name = "LoginServlet", urlPatterns = {"/login"})
public class LoginServlet extends HttpServlet {

    private void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String userName = request.getParameter("user_name");
        String password = request.getParameter("password");

        //Creamos una session
        HttpSession session = request.getSession(true);
        session.setMaxInactiveInterval(60*15); //Estaablecemos el tiempo que durara viva la session
        System.out.println("Registering User");
        LoginService loginService = new LoginService(); //
        Usuario usuario = new Usuario();
        usuario.setUserName(userName);
        usuario.setPassword(password);
        String result = loginService.authenticateUser(usuario);
        if (result.equals(LoginService.SUCCSES)) {
            usuario = loginService.getAuthenticatedUser();
            
            //Session
            session.setAttribute("user", usuario);
            session.setAttribute("last_update", new Date()); //Fecha de ultima vez actualizado los tweets para el usuario
            
            //Redireccinamos al ServletHome para que el se encarge de procesar la pagina de inicio
            response.sendRedirect("home");

            //request.setAttribute("ola", "ola");
            //request.getRequestDispatcher("/home").forward(request, response);
        } else {
            request.setAttribute("message", result);
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
        //}
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
//    @Override
//    protected void doGet(HttpServletRequest request, HttpServletResponse response)
//            throws ServletException, IOException {
//        response.sendRedirect("welcome.jsp");
//    }
}
